package com.wing.cabinet.controller.websocket;

import com.alibaba.fastjson.JSONObject;
import com.wing.cabinet.model.response.CabinetVO;
import com.wing.cabinet.service.CabinetService;
import com.wing.common.utils.JsonResult;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController("CabinetOpenController")
@RequestMapping("cabinet/websocket/cabinetOpen")
@Slf4j
public class CabinetOpenController {

    @Autowired
    private CabinetService cabinetService;

    @GetMapping("/open")
    public JsonResult openCab( String cabCode,String spaceCode){
       /* cabCode = "CD01";
        spaceCode = "7";*/
        String enterpriseCode= JwtUtils.getEnterpriseCode();
        CabinetVO cabinetVO = cabinetService.get4appByCode(cabCode);

        try{
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("cabCode",cabCode);
            jsonObject.put("spaceCode",spaceCode);
            jsonObject.put("type","remoteOpen");

            CabinetOpenServer.sendMessage(enterpriseCode,cabinetVO.getBelongCabinet(), jsonObject.toJSONString());
        }catch (Exception e){
            log.error("send fail",e);
            return JsonResult.fail("开柜失败");
        }
        return JsonResult.success();
    }


}
